package com.zl.learning.thread.concurrent;

public class VisibiltyDemo1 {

    static class MyThread extends Thread {

        private volatile boolean flag = false;

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = true;
            System.out.println("flag=" + flag);
        }

        public boolean isFlag() {
            return flag;
        }
    }

    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start();
        long count = 0;
        while (true) {
            if (t.isFlag()) {
                System.out.println("主线程进入循环执行》。。");
                break;
            }
            count++;
//            try {
//                Thread.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        //3678733052
        System.out.println(count);
    }
}
